package controller

import (
	"fx_swift_forex/api"
	"fx_swift_forex/internal/service"
	"fx_swift_forex/pkg/code"
	"fx_swift_forex/pkg/internalerrors"
	"github.com/gin-gonic/gin"
	"net/http"
)

// 新增/更新/删除收益人
func OperatorBeneficiaries(c *gin.Context) {
	req := &api.OperatorBeneficiariesReq{}
	if err := c.ShouldBindJSON(&req); err != nil {
		code.Fail(c, http.StatusBadRequest, code.ErrEncodingJSON, err.Error())
		return
	}
	err := service.NewBeneficiariesSrv().OperatorBeneficiaries(c, req)
	if err != nil {
		code.Fail(c, http.StatusBadRequest, err.(internalerrors.ServiceError).Code, err.(internalerrors.ServiceError).Msg)
		return
	} else {
		code.Success(c, nil)
	}
}

// 校验受益人银行详情
func ValidateBeneficiaries(c *gin.Context) {
	req := &api.ValidateBeneficiariesReq{}
	if err := c.ShouldBindJSON(&req); err != nil {
		code.Fail(c, http.StatusBadRequest, code.ErrEncodingJSON, err.Error())
		return
	}
	result, err := service.NewBeneficiariesSrv().ValidateBeneficiaries(c, req)
	if err != nil {
		code.Fail(c, http.StatusBadRequest, err.(internalerrors.ServiceError).Code, err.(internalerrors.ServiceError).Msg)
		return
	} else {
		code.Success(c, result)
	}
}

// 获取受益人要求
func GetBeneficiaryRequirements(c *gin.Context) {
	req := &api.GetBeneficiaryRequirementsReq{}
	if err := c.ShouldBindJSON(&req); err != nil {
		code.Fail(c, http.StatusBadRequest, code.ErrEncodingJSON, err.Error())
		return
	}
	result, err := service.NewBeneficiariesSrv().GetBeneficiaryRequirements(c, req)
	if err != nil {
		code.Fail(c, http.StatusBadRequest, err.(internalerrors.ServiceError).Code, err.(internalerrors.ServiceError).Msg)
		return
	} else {
		code.Success(c, result)
	}
}

// 通过受益人ID查询信息
func BeneficiaryIdToInfo(c *gin.Context) {
	beneficiaryId := c.Query("beneficiary_id")
	result, err := service.NewBeneficiariesSrv().GetBeneficiaryIdToInfo(c, beneficiaryId)
	if err != nil {
		code.Fail(c, http.StatusBadRequest, err.(internalerrors.ServiceError).Code, err.(internalerrors.ServiceError).Msg)
		return
	} else {
		code.Success(c, result)
	}
}

// 获取受益人要求
func BeneficiaryList(c *gin.Context) {
	req := &api.BeneficiaryListReq{}
	if err := c.ShouldBindJSON(&req); err != nil {
		code.Fail(c, http.StatusBadRequest, code.ErrEncodingJSON, err.Error())
		return
	}
	result, err := service.NewBeneficiariesSrv().BeneficiaryList(c, req)
	if err != nil {
		code.Fail(c, http.StatusBadRequest, err.(internalerrors.ServiceError).Code, err.(internalerrors.ServiceError).Msg)
		return
	} else {
		code.Success(c, result)
	}
}

// 修改受益人昵称
func EditNickName(c *gin.Context) {
	req := &api.EditBeneficiaryNickNameReq{}
	if err := c.ShouldBindJSON(&req); err != nil {
		code.Fail(c, http.StatusBadRequest, code.ErrEncodingJSON, err.Error())
		return
	}
	err := service.NewBeneficiariesSrv().EditBeneficiaryNickName(c, req)
	if err != nil {
		code.Fail(c, http.StatusBadRequest, err.(internalerrors.ServiceError).Code, err.(internalerrors.ServiceError).Msg)
		return
	} else {
		code.Success(c, nil)
	}
}
